<?php

/**
 * Determine whether the currently logged user has access to this resource
 *
 * @category Ext
 * @package Ext_Controller
 * @subpackage Action_Helper
 */
class Ext_Controller_Action_Helper_Acl extends Zend_Controller_Action_Helper_Abstract
{

    /**
     * Default role when unable to define current role
     */
    const DEFAULT_ROLE = 0;

    /**
     * @var Zend_Acl_Role_Inferface|string $role
     */
    protected $_role;

    /**
     * @var Zend_Acl
     */
    protected $_acl;

    public function direct($resource, $privilege = null)
    {
        return $this->isAllowed($resource, $privilege);
    }

    public function isAllowed($resource, $privilege = null)
    {
        $allowed = $this->getAcl()->isAllowed($this->getRole(), $resource, $privilege);

        return $allowed;
    }

    /**
     * @param Zend_Acl_Role_Inferface|string $role
     * @return Ext_Controller_Action_Helper_Acl
     */
    public function setRole($role)
    {
        $this->_role = $role;

        return $this;
    }

    public function getRole()
    {
        if (null === $this->_role) {
            $auth = Zend_Auth::getInstance();
            if ($auth->hasIdentity()) {
                $identity = $auth->getIdentity();
                if (is_array($identity)) {
                    if (isset($identity['role'])) {
                        $role = $identity['role'];
                    }
                } else {
                    $role = self::DEFAULT_ROLE;
                }
            } else {

                $role = self::DEFAULT_ROLE;
            }

            $this->setRole($role);

            return $role;
        }

        return $this->_role;
    }

    /**
     * @throws Exception
     * @return Zend_Acl
     */
    public function getAcl()
    {

        if (null === $this->_acl) {
            $front = $this->getFrontController();
            $bootstrap = $front->getParam('bootstrap');
            if ($bootstrap) {
                $resourceName = 'acl';
                if ($bootstrap->hasResource($resourceName)) {
                    $acl = $bootstrap->getResource($resourceName);
                } else {
                    throw new Exception('Unable to find ' . $resourceName . ' resource');
                }
            } else {
                throw new Exception('Unable to find bootstrap');
            }

            $this->_acl = $acl;
        }

        return $this->_acl;
    }

    /**
     * @param Zend_Acl $acl
     * @return Ext_Controller_Action_Helper_Acl
     */
    public function setAcl(Zend_Acl $acl)
    {
        $this->_acl = $acl;

        return $this;
    }
}
